<?php
	require_once dirname(BASEPATH) . '/application/controllers/system/_init.php';
	
	abstract class abs_page extends _init
	{		
		// Page section		
		private function check_cafe_state(){
			$this->load->model('local/data/cafe/mod_cafe');
			return $this->mod_cafe->check_cafe_state(get_class($this));
		}
		private function pre_page_data($active_menu){
			$this->template->set_template('right_column');			
			$this->set_header_main();
			
			$this->load->model('local/data/cafe/mod_cafe');
			
			$this->set_navigation(array('<i class="icon-home"></i>หน้าแรก' => base_url(),
										'รายชื่อร้านกาแฟ' => base_url() . 'cafe',
										$this->mod_cafe->get_cafe_title_by_name(get_class($this)) => base_url() . get_class($this)));
						
			// load cover data			
			$this->load->model('local/data/cafe/mod_cafe_product_category');
			
			$row = $this->mod_cafe->get_single_cafe_by_name(get_class($this), array('CAF_NAME','CAF_TITLE','CAF_IMG_L_FILENAME','CAF_IMG_C_FILENAME','CAF_WEBSITE','CAF_FACEBOOK','CAF_FAV_NUM'));			
			$data_content_0['cafe'] = $row;
			$data_content_0['product_category'] = $this->mod_cafe_product_category->get_category_list(true);
			$data_content_0['cafe_name'] = get_class($this);
			$data_content_0['active_menu'] = $active_menu;			
				
			// load header data			
			$data_content_1['data'] = $row;
			$data_content_1['favorited'] = $this->mod_cafe->check_is_favorited_cafe($this->cafe_id(), $this->session->userdata('MEM_ID'));
			$data_content_1['favorite_count'] = $row['CAF_FAV_NUM'];
							
			// load cafe type
			$this->load->model('local/data/cafe/mod_cafe_type_list');			
			$data_content_1['types'] = $this->mod_cafe_type_list->get_typelist_of_cafe($this->cafe_id(), true);

			$this->template->write('content_1', $this->load->view('cafe/frm_cover', $data_content_0, true));
			$this->template->write('right_sidebar', $this->load->view('widget/page/frm_info', $data_content_1, true));
		}
		private function offline(){
			$this->set_header_main();
			$this->set_navigation(array('<i class="icon-home"></i>หน้าแรก' => base_url(),
										'รายชื่อร้านกาแฟ' => base_url() . 'cafe'));
			$this->template->write('content_1', $this->load->view('cafe/frm_offline', null, true));
			$this->template->render();			
		}
		
		function index(){
			if ($this->check_cafe_state()){
				$this->pre_page_data('index');	
				
				$this->load->model('local/data/cafe/mod_cafe');
				$data_content['data'] = $this->mod_cafe->get_cafe_about(get_class($this));
	
				$this->template->write('content_1', $this->load->view('cafe/frm_about', $data_content, true));
				$this->template->render();
			} else {
				$this->offline();
			}
		}
		function gallery(){
			if ($this->check_cafe_state()){
				$this->pre_page_data('gallery');			
							
				$this->load->model('local/data/cafe/mod_cafe_img_gallery');
				$data_content['data'] = $this->mod_cafe_img_gallery->get_gallery_list($this->cafe_id());
				
				$this->template->write('content_1', $this->load->view('cafe/frm_gallery', $data_content, true));
				$this->template->render();
			} else {
				
			}
		}
		function product(){

			if ($this->check_cafe_state()){
				$opr = $this->uri->segment(3);
				switch ($opr){
					case 'view':{	
	
						$opr = $this->uri->segment(4);
						switch ($opr){
							case 'ajax_rate':{
								
								$this->load->library('define/operation_message');					
								if ($this->check_member_session()){								
									print $this->operation_message->msg_no_member_login;
								} else {
									$message = null;
									
									$score = $this->input->post('v');
									if (($score < 0) && ($score > 5) && is_numeric($score))
										$score = 0;
									
									$this->load->model('local/data/cafe/mod_cafe_product');
									$product_id = $this->mod_cafe_product->get_product_id_from_product_code($this->input->post('pc'));
									
									$this->load->model('local/data/rate/mod_p_rate_record');
									if ($this->mod_p_rate_record->rate($product_id, $this->session->userdata('MEM_ID'), $this->cafe_id(), $score))
										$message = $this->operation_message->msg_rate_success;
									else
										$message = $this->operation_message->msg_rate_fail;
																		
									print $message;
								}
							}
							break;
							default:{
								$this->set_header_main();
								$this->set_navigation(array('<i class="icon-home"></i>หน้าแรก' => base_url(),
															'รายชื่อร้านกาแฟ' => base_url() . 'cafe/',
															$this->mod_cafe->get_cafe_title_by_name(get_class($this)) => base_url() . get_class($this)
															));
								
								$this->load->model('local/data/cafe/mod_cafe_product');
								$this->load->model('local/data/etc/mod_banner');
								$this->load->model('local/data/rate/mod_p_rate_record');
								
								$data_content_0['item'] = $this->mod_cafe_product->get_single_product($this->uri->segment(4));
								$data_content_0['cafe_name'] = get_class($this);		
								$data_content_0['user_rate'] = $this->mod_p_rate_record->get_rate_record_of_member($this->mod_cafe_product->get_product_id_from_product_code($this->uri->segment(4)), $this->session->userdata('MEM_ID'));															
								$data_content_1['data'] = $this->mod_banner->get_banner_product_view_zone();
								$data_content_2['data'] = $this->mod_cafe_product->get_product_related($this->cafe_id());
								$data_content_2['cafe_name'] = get_class($this);				
								
								$this->template->write('content_1', $this->load->view('cafe/frm_product_view', $data_content_0, true));
								$this->template->write('right_sidebar', $this->load->view('widget/main/frm_banner_vertical', $data_content_1, true));
								$this->template->write('right_sidebar', $this->load->view('widget/page/frm_product_related', $data_content_2, true));
								$this->template->render();		
							}
							break;
						}					
					}
					break;
					default:{
						$this->pre_page_data('product');
						
						$category_id = $this->input->get('c');
						$start_index = $this->input->get('p');
						$display_length = 9;
						
						if (empty($category_id))
							$category_id = null;
						if (empty($start_index))
							$start_index = null;
						else{
							$start_index = ($start_index -1) * $display_length;
							if ($start_index < 0)
								$start_index = 0;
						}
						
						$cafe_id = $this->cafe_id();
						
						// product list					
						$this->load->model('local/data/cafe/mod_cafe_product');
						$data_content['data'] = $this->mod_cafe_product->get_product_list($cafe_id, $category_id, $display_length, $start_index);
						$data_content['max_count'] = ceil($this->mod_cafe_product->get_count($cafe_id, $category_id) / $display_length);					
						$data_content['cafe_name'] = get_class($this);
						$data_content['current_category'] = $category_id;					
						
						$this->template->write('content_1', $this->load->view('cafe/frm_product', $data_content, true));
						$this->template->render();
					}
					break;
				}		
			} else {
				
			}
		}
		function contact(){
			if ($this->check_cafe_state()){
				$this->pre_page_data('contact');
				
				$this->load->model('local/data/cafe/mod_cafe_contact');
				$data_content['data'] = $this->mod_cafe_contact->get_contact_of_cafe($this->cafe_id());
				
				$this->template->write('content_1', $this->load->view('cafe/frm_contact', $data_content, true));
				$this->template->render();
			} else {
				
			}
		}		
			
		// Backend section
		protected function check_member_session(){
			if (parent::check_member_session()){
				
				$this->load->model('local/data/cafe/mod_cafe');
				if ($this->mod_cafe->check_cafe_owner(get_class($this), $this->session->userdata('MEM_ID')))
					return 1;
				else 
					return -2;				
			} else 
				return 0;
		}	
		private function pre_backend_data($active_menu){
			$this->template->set_template('right_column');
			
			$this->load->model('local/data/cafe/mod_cafe');
			
			// load header data
			$fields = array('CAF_NAME','CAF_TITLE','CAF_IMG_L_FILENAME');
			$data_header['data'] = $this->mod_cafe->get_single_cafe_by_name(get_class($this), $fields);			
						
			$data_menu['cafe_name'] = get_class($this);
			$data_menu['active_menu'] = $active_menu;						
			
			$this->set_header_main(true, false, true);
			$this->template->write('header', $this->load->view('template/component/page/backend/frm_backend_header', null, true));
			$this->template->write('right_sidebar', $this->load->view('template/component/page/backend/frm_backend_menu_sidebar', $data_menu, true));		
			
		}
		function backend(){
			$opr = $this->uri->segment(3);
			
			switch ($opr){				
				case 'general': default:{
					$opr = $this->uri->segment(4);
					$this->region_general($opr);
				}
				break;
				case 'product':{
					$opr = $this->uri->segment(4);
					$this->region_product($opr);
				}	
				break;
				case 'gallery':{
					$opr = $this->uri->segment(4);
					$this->region_gallery($opr);						
				}	
				break;	
				case 'package':{
					$opr = $this->uri->segment(4);	
					$this->region_package($opr);									
				}
				break;						
			}					
		}		
				
		// Backend region function		
		private function region_general($opr){
			switch ($opr){
				case 'info': default:{
					
					$opr = $this->uri->segment(5);
					switch ($opr){
						case 'ajax_save':{							
							$result = 0;
							$message = null;
							$redirect_url = null;

							$this->load->library('define/operation_message');							
		
							switch (trim($this->check_member_session())){
								
								case 1:{
									$this->load->model('local/data/cafe/mod_cafe');
									$this->mod_cafe->CAF_TITLE = $this->input->post('tct');
									$this->mod_cafe->CAF_FACEBOOK = $this->input->post('tf');
									$this->mod_cafe->CAF_WEBSITE = $this->input->post('tw');
									
									$cafe_id = $this->cafe_id();							
									
									if ($this->mod_cafe->edit($cafe_id)){
										$this->load->model('local/data/cafe/mod_cafe_type_list');
										$this->mod_cafe_type_list->CAF_T_L_LIST = implode(',', $this->input->post('chk-ct'));
										$this->mod_cafe_type_list->CAF_ID = $cafe_id;
										
										if ($this->mod_cafe_type_list->add()){
											$result = 1;
											$message = $this->operation_message->msg_save_success;
										} else 
											$message = $this->operation_message->msg_save_fail;						
									} else
										$message = $this->operation_message->msg_save_fail;									
								}
								break;
								case 0: default:{
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';									
								}
								break;
								case -2:{
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';									
								}
								break;								
							}
							
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));			
						}
						break;
						case 'ajax_change_state':{
							$result = 0;
							$message = null;
							$redirect_url = null;
							
							$this->load->library('define/operation_message');							
							switch ($this->check_member_session()){
								
								case 1:{									
									$status = $this->input->post('s');
									if ($status == 'true'){
										if ($this->mod_cafe->change_cafe_status($this->cafe_id(), true)){
											$result = 1;
											$message = $this->operation_message->msg_save_success;
										} else
											$message = $this->operation_message->msg_save_fail;
																		
									}else{
										if ($this->mod_cafe->change_cafe_status($this->cafe_id(), false)){
											$result = 1;
											$message = $this->operation_message->msg_save_success;
										} else
											$message = $this->operation_message->msg_save_fail;
									}
								}
								break;
								case 0: default:{
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2:{
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
							}					

							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));
							
						}
						break;
						default:{
																										
							if ($this->check_member_session() === 1){															
								$this->pre_backend_data('general');
								$this->set_navigation(array('ระบบจัดการร้าน ' . $this->uri->segment(1) => base_url() . $this->uri->segment(1) . '/backend',
															'ข้อมูลทั่วไป' => base_url() . $this->uri->segment(1) . '/backend/general'));
														
														
								$this->load->model('local/data/cafe/mod_cafe_type');
								$this->load->model('local/data/cafe/mod_cafe');							
								
								$cafe_detail = $this->mod_cafe->get_single_cafe_by_name(get_class($this), array('CAF_TITLE', 'CAF_FACEBOOK', 'CAF_WEBSITE', 'CAF_STATUS'));
								$data_content['cafe_title'] = $cafe_detail['CAF_TITLE'];
								$data_content['cafe_facebook'] = $cafe_detail['CAF_FACEBOOK'];
								$data_content['cafe_website'] = $cafe_detail['CAF_WEBSITE'];
								$data_content['cafe_status'] = $cafe_detail['CAF_STATUS'];							
								
								$data_content['data'] = $this->mod_cafe_type->get_cafe_type_list($this->cafe_id());
								$data_content['cafe_name'] = get_class($this);
									
								$this->template->write('content_2', $this->load->view('cafe/backend/frm_backend_general_info', $data_content, true));
								$this->template->render();
							} else 
								redirect(base_url() . 'member/logout');							
						}
						break;	
					}					
				}
				break;
				case 'about':{

					$opr = $this->uri->segment(5);
					switch ($opr){
						case 'ajax_save':{
														
							$result = 0;
							$message = null;
							$redirect_url = null;

							$this->load->library('define/operation_message');
							
							switch ($this->check_member_session()){
								
								case 1:{
																
									$this->load->model('local/data/cafe/mod_cafe');								
									if ($this->mod_cafe->save_cafe_about($this->input->post('tc'), $this->cafe_id())){
										$result = true;
										$message = $this->operation_message->msg_save_success;
									} else								
										$message = $this->operation_message->msg_save_fail;
								}
								break;
								case 0: default:{									
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2: {									
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;								
							}
							
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));
						}
						break;
						default:{

							if ($this->check_member_session() === 1){								
								$this->pre_backend_data('general/about');
								$this->set_navigation(array('ระบบจัดการร้าน ' . $this->uri->segment(1) => base_url() . $this->uri->segment(1) . '/backend',
															'ข้อมูลทั่วไป' => base_url() . $this->uri->segment(1) . '/backend/general',
															'เกี่ยวกับร้าน' => base_url() . $this->uri->segment(1) . '/backend/general/about/'));
															
								$this->load->model('local/data/cafe/mod_cafe');
								$data_content['intro_text'] = $this->mod_cafe->get_cafe_about(get_class($this));
								$data_content['cafe_name'] = get_class($this);
									
								$this->template->write('content_2', $this->load->view('cafe/backend/frm_backend_general_about', $data_content, true));
								$this->template->render();
							} else 
								redirect(base_url() . 'member/logout');
						}
						break;
					}
				}
				break;
				case 'logo':{
					
					$opr = $this->uri->segment(5);
					switch ($opr){
						case 'add':{
							$this->load->library('define/operation_message');
							
							if ($this->check_member_session() === 1){
								$file = $_FILES['fl'];
								if (!empty($file['name'])){								
									$message = null;									
									$cafe_id = $this->cafe_id();
										
									// get current package detail
									$this->load->model('local/data/cafe/mod_cafe_expire');
									$package = $this->mod_cafe_expire->get_current_package_state($cafe_id, true, true);
										
									$img_limit = $package['CAF_LOGO_COUNT'];
										
									// get current image gallery count
									$this->load->model('local/data/cafe/mod_cafe_img_logo');
									$curr_count = $this->mod_cafe_img_logo->get_current_img_count($cafe_id);
										
									// check if cafe image count over limitation of pakcage
									if ($curr_count < $img_limit){
										$this->load->library('imageupload');
										$this->load->library('define/image_upload_define');
										$this->load->library('define/directory_path');
								
										$this->imageupload->target_directory = $this->directory_path->img_upload_logo_path;
										$this->imageupload->max_image_height = $this->image_upload_define->logo_img_limit_height;
										$this->imageupload->max_image_width = $this->image_upload_define->logo_img_limit_width;
										$this->imageupload->max_image_size = $this->image_upload_define->logo_img_limit_size;
										$this->imageupload->image_extensions = $this->image_upload_define->logo_img_extensions;
										$this->imageupload->prefix = $cafe_id;
											
										if ($this->imageupload->upload($file)){
											$this->load->model('local/data/cafe/mod_cafe_img_logo');
											$this->mod_cafe_img_logo->CAF_IMG_L_FILENAME = $this->imageupload->encode_filename($file['name']);
											$this->mod_cafe_img_logo->CAF_ID = $cafe_id;
											if (!$this->mod_cafe_img_logo->add())
												$message = $this->operation_message->msg_save_fail;
											else
												$this->mod_cafe->set_last_active(get_class($this));
										}else
											$message = $this->imageupload->error_message;
									}else
										$message = $this->operation_message->msg_image_over_limit . ' ' . $img_limit . ' รูป';
								}else
									$message = null;
								
								$this->jscript_redirect(base_url() . get_class($this) . '/backend/general/logo/', $message);
								
							} else
								$this->jscript_redirect(base_url() . get_class($this) . '/backend/general/logo/', $this->operation_message->msg_relogin);
						}
						break;
						case 'ajax_remove_item':{
							$result = 0;
							$message = null;
							$redirect_url = null;
							
							$this->load->library('define/operation_message');
							
							switch ($this->check_member_session()){
								
								case 1:{																
									$filename = $this->input->post('fn');							
									$this->load->model('local/data/cafe/mod_cafe_img_logo');								
									
									if ($this->mod_cafe_img_logo->remove_by_filename($filename)){
										$result = 1;
										$message = $this->operation_message->msg_remove_success;
									} else									
										$message = $this->operation_message->msg_remove_fail;
								}
								break;
								case 0: default:{									
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2: {									
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;								
							}
														
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));
						}
						break;
						case 'ajax_select_item':{
							
							$result = 0;
							$message = null;
							$redirect_url = null;							
							
							$this->load->library('define/operation_message');
							switch ($this->check_member_session()){
								
								case 1:{																
									$filename = $this->input->post('fn');
								
									$this->load->model('local/data/cafe/mod_cafe_img_logo');
									$logo_id = $this->mod_cafe_img_logo->get_id_by_filename($filename);
									
									if (!is_null($logo_id)){
										$this->load->model('local/data/cafe/mod_cafe');									
										
										if ($this->mod_cafe->set_selected_logo($logo_id, get_class($this))){
											$result = 1;
											$message = $this->operation_message->msg_save_success;	
										}else										
											$message = $this->operation_message->msg_no_logo_found;								
									}	
								}
								break;
								case 0: default:{									
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2: {									
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;								
							}

							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));
						}
						break;
						default:{	

							if ($this->check_member_session() === 1){								
								$this->pre_backend_data('general/logo');
								$this->set_navigation(array('ระบบจัดการร้าน ' . $this->uri->segment(1) => base_url() . $this->uri->segment(1) . '/backend',
															'ข้อมูลทั่วไป' => base_url() . $this->uri->segment(1) . '/backend/general',
															'รูปภาพโลโก้' => base_url() . $this->uri->segment(1) . '/backend/general/logo/'));
								
								$this->load->model('local/data/cafe/mod_cafe');
								$curr_logo = $this->mod_cafe->get_cafe_current_logo(get_class($this));
								
								// load logo list
								$this->load->model('local/data/cafe/mod_cafe_img_logo');
								$cafe_logo_list = $this->mod_cafe_img_logo->get_logo_list($this->cafe_id());
								
								$arr_logo = array();
								foreach ($cafe_logo_list as $row)
								{
									if ($row['CAF_IMG_L_ID'] == $curr_logo['CAF_IMG_L_ID'])
										array_push($arr_logo, array('IMG_FILE' => $row['CAF_IMG_L_FILENAME'],
												'IN_USE' => true));
									else
										array_push($arr_logo, array('IMG_FILE' => $row['CAF_IMG_L_FILENAME'],
												'IN_USE' => false));
								}						
								
								$this->load->library('define/image_upload_define');
								
								$data_content['cafe_name'] = get_class($this);
								$data_content['data'] = $arr_logo;
								$data_content['image_config'] = array('logo_img_limit_height' => $this->image_upload_define->logo_img_limit_height,
																	'logo_img_limit_width' => $this->image_upload_define->logo_img_limit_width,
																	'logo_img_limit_size' => $this->image_upload_define->logo_img_limit_size,
																	'logo_img_extensions' => $this->image_upload_define->logo_img_extensions);
								
								$this->template->write('content_2', $this->load->view('cafe/backend/frm_backend_general_logo', $data_content, true));
								$this->template->render();
							} else
								redirect(base_url() . 'member/logout');
						}
						break;
					}					
				}
				break;
				case 'cover':{
					
					$opr = $this->uri->segment(5);
					switch ($opr){
						case 'add':{
							$this->load->library('define/operation_message');
							
							if ($this->check_member_session() === 1){
								
								$file = $_FILES['fc'];						
								if (!empty($file['name'])){																			
									$message = null;									
									$cafe_id = $this->cafe_id();
						
									// get current package detail
									$this->load->model('local/data/cafe/mod_cafe_expire');
									$package = $this->mod_cafe_expire->get_current_package_state($cafe_id, true, true);
										
									$img_limit = $package['CAF_COVER_COUNT'];
										
									// get current image gallery count
									$this->load->model('local/data/cafe/mod_cafe_img_cover');
									$curr_count = $this->mod_cafe_img_cover->get_current_img_count($cafe_id);
						
									// check if cafe image count over limitation of pakcage
									if ($curr_count < $img_limit){
										$this->load->library('imageupload');
										$this->load->library('define/image_upload_define');
										$this->load->library('define/directory_path');
											
										$this->imageupload->target_directory = $this->directory_path->img_upload_cover_path;
										$this->imageupload->max_image_height = $this->image_upload_define->cover_img_limit_height;
										$this->imageupload->max_image_width = $this->image_upload_define->cover_img_limit_width;
										$this->imageupload->max_image_size = $this->image_upload_define->cover_img_limit_size;
										$this->imageupload->image_extensions = $this->image_upload_define->cover_img_extensions;
										$this->imageupload->prefix = $cafe_id;
						
										if ($this->imageupload->upload($file)){
											$this->load->model("local/data/cafe/mod_cafe_img_cover");
											$this->mod_cafe_img_cover->CAF_IMG_C_FILENAME = $this->imageupload->encode_filename($file['name']);
											$this->mod_cafe_img_cover->CAF_ID = $cafe_id;
											if (!$this->mod_cafe_img_cover->add())
												$message = $this->operation_message->msg_save_fail;
											else
												$this->mod_cafe->set_last_active(get_class($this));									
										}else
											$message = $this->imageupload->error_message;
									}else
										$message = $this->operation_message->msg_image_over_limit . ' ' . $img_limit . ' รูป';
								}else
									$message = null;
									
								$this->jscript_redirect(base_url() . get_class($this) . '/backend/general/cover/', $message);
							} else 
								$this->jscript_redirect(base_url() . get_class($this) . '/backend/general/cover/', $this->operation_message->msg_relogin);						
						}
						break;
						case 'ajax_remove_item':{
							$result = 0;
							$message = null;
							$redirect_url = null;
							
							$this->load->library('define/operation_message');							
							switch ($this->check_member_session()){
								
								case 1:{																
									$filename = $this->input->post('fn');
								
									$this->load->model('local/data/cafe/mod_cafe_img_cover');								
									if ($this->mod_cafe_img_cover->remove_by_filename($filename)){
										$result = 1;
										$message = $this->operation_message->msg_save_success;
									} else
										$message = $this->operation_message->msg_save_fail;
								}
								break;
								case 0: default:{									
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2: {									
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;								
							}
							
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));
						}
						break;
						case 'ajax_select_item':{
							
							$result = 0;
							$message = null;
							$redirect_url = null;
							
							$this->load->library('define/operation_message');							
							switch ($this->check_member_session()){
								
								case 1:{																
									$filename = $this->input->post('fn');
	
									$this->load->model('local/data/cafe/mod_cafe_img_cover');
									$cover_id = $this->mod_cafe_img_cover->get_id_by_filename($filename);
									
									if (!is_null($cover_id)){
										$this->load->model('local/data/cafe/mod_cafe');
										if ($this->mod_cafe->set_selected_cover($cover_id, get_class($this)))
											$result = 1;
										else{
											$this->load->library('define/operation_message');											
											$message = $this->operation_message->msg_no_cover_found;
										}
									}
								}
								break;
								case 0: default:{									
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2: {									
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;								
							}

							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));
						}
						break;
						default:{	

							if ($this->check_member_session() === 1){								
								$this->pre_backend_data('general/cover');
								$this->set_navigation(array('ระบบจัดการร้าน ' . $this->uri->segment(1) => base_url() . $this->uri->segment(1) . '/backend',
															'ข้อมูลทั่วไป' => base_url() . $this->uri->segment(1) . '/backend/general',
															'รูปภาพ Cover' => base_url() . $this->uri->segment(1) . '/backend/general/cover/'));
															
								$this->load->model('local/data/cafe/mod_cafe');
								$curr_cover = $this->mod_cafe->get_cafe_current_cover(get_class($this));
								
								// load cover list
								$this->load->model('local/data/cafe/mod_cafe_img_cover');
								$cafe_cover_list = $this->mod_cafe_img_cover->get_cover_list($this->cafe_id());
								
								$arr_cover = array();
								foreach ($cafe_cover_list as $row)
								{
									if ($row['CAF_IMG_C_ID'] == $curr_cover['CAF_IMG_C_ID'])
										array_push($arr_cover, array('IMG_FILE' => $row['CAF_IMG_C_FILENAME'],
												'IN_USE' => true));
									else
										array_push($arr_cover, array('IMG_FILE' => $row['CAF_IMG_C_FILENAME'],
												'IN_USE' => false));
								}
								
								$this->load->library('define/image_upload_define');
								$data_content['cafe_name'] = get_class($this);
								$data_content['data'] = $arr_cover;
								$data_content['image_config'] = array('cover_img_limit_height' => $this->image_upload_define->cover_img_limit_height,
																	'cover_img_limit_width' => $this->image_upload_define->cover_img_limit_width,
																	'cover_img_limit_size' => $this->image_upload_define->cover_img_limit_size,
																	'cover_img_extensions' => $this->image_upload_define->cover_img_extensions);
								
								$this->template->write('content_2', $this->load->view('cafe/backend/frm_backend_general_cover', $data_content, true));
								$this->template->render();
							} else
								redirect(base_url() . 'member/logout');
						}
						break;
					}				
				}
				break;
				case 'contact':{
						
					$opr = $this->uri->segment('5');
					switch ($opr){
						case 'ajax_add':{
							
							$result = 0;
							$message = null;
							$redirect_url = null;
							
							$this->load->library('define/operation_message');
							switch ($this->check_member_session()){
								
								case 1:{									
									$cafe_id = $this->cafe_id();
							
									$this->load->model('local/data/cafe/mod_cafe_contact');
									$this->mod_cafe_contact->CAF_P_L_TITLE = $this->input->post('cct');
									$this->mod_cafe_contact->CAF_P_L_ADDRESS = $this->input->post('cca');
									$this->mod_cafe_contact->PROVINCE_ID = $this->input->post('ccpv');
									$this->mod_cafe_contact->CAF_P_L_POSTCODE = $this->input->post('ccpc');
									$this->mod_cafe_contact->CAF_P_L_PHONE = $this->input->post('ccph');
									$this->mod_cafe_contact->CAF_P_L_FAX = $this->input->post('ccfx');
									$this->mod_cafe_contact->CAF_P_L_EMAIL = $this->input->post('ccem');
									$this->mod_cafe_contact->CAF_ID = $cafe_id;														
											
									if (!empty($_FILES['fm']['name']) && isset($_FILES['fm'])){								
										$file = $_FILES['fm'];
										
										$this->load->library('imageupload');
										$this->load->library('define/image_upload_define');
										$this->load->library('define/directory_path');
												
										$this->imageupload->target_directory = $this->directory_path->img_upload_cafe_map;
										$this->imageupload->max_image_height = $this->image_upload_define->map_img_limit_height;
										$this->imageupload->max_image_width = $this->image_upload_define->map_img_limit_width;
										$this->imageupload->max_image_size = $this->image_upload_define->map_img_limit_size;
										$this->imageupload->image_extensions = $this->image_upload_define->map_img_extensions;
										$this->imageupload->prefix = $cafe_id;
							
										if ($this->imageupload->upload($file)){
											$this->mod_cafe_contact->CAF_P_L_MAP = $this->imageupload->encode_filename($file['name']);
											
											if ($this->mod_cafe_contact->add()){
												$result = 1;
												$message = $this->operation_message->msg_save_success;
											} else
												$message = $this->operation_message->msg_save_fail;									
										}else
											$message = $this->imageupload->error_message;									
									} else {
										if ($this->mod_cafe_contact->add()){
											$result = 1;
											$message = $this->operation_message->msg_save_success;
										}else
											$message = $this->operation_message->msg_save_fail;
									}
								}
								break;
								case 0: default:{									
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2: {									
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;								
							}

							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));								
						}
						break;
						case 'ajax_edit':{
							
							$result = 0;
							$message = null;
							$redirect_url = null;			
							
							$this->load->library('define/operation_message');
							switch ($this->check_member_session()){
								
								case 1:{									
									$cafe_id = $this->cafe_id();
									
									$this->load->model('local/data/cafe/mod_cafe_contact');
									$this->mod_cafe_contact->CAF_P_L_TITLE = $this->input->post('cct');
									$this->mod_cafe_contact->CAF_P_L_ADDRESS = $this->input->post('cca');
									$this->mod_cafe_contact->PROVINCE_ID = $this->input->post('ccpv');
									$this->mod_cafe_contact->CAF_P_L_POSTCODE = $this->input->post('ccpc');
									$this->mod_cafe_contact->CAF_P_L_PHONE = $this->input->post('ccph');
									$this->mod_cafe_contact->CAF_P_L_FAX = $this->input->post('ccfx');
									$this->mod_cafe_contact->CAF_P_L_EMAIL = $this->input->post('ccem');
									$this->mod_cafe_contact->CAF_ID = $cafe_id;														
											
									if (!empty($_FILES['fm']['name']) && isset($_FILES['fm'])){								
										$file = $_FILES['fm'];
										
										$this->load->library('imageupload');
										$this->load->library('define/image_upload_define');
										$this->load->library('define/directory_path');
												
										$this->imageupload->target_directory = $this->directory_path->img_upload_cafe_map;
										$this->imageupload->max_image_height = $this->image_upload_define->map_img_limit_height;
										$this->imageupload->max_image_width = $this->image_upload_define->map_img_limit_width;
										$this->imageupload->max_image_size = $this->image_upload_define->map_img_limit_size;
										$this->imageupload->image_extensions = $this->image_upload_define->map_img_extensions;
										$this->imageupload->prefix = $cafe_id;
							
										if ($this->imageupload->upload($file)){
											$this->mod_cafe_contact->CAF_P_L_MAP = $this->imageupload->encode_filename($file['name']);
											
											if ($this->mod_cafe_contact->edit($this->input->post('ccid'), $cafe_id)){
												$result = 1;
												$message = $this->operation_message->msg_save_success;
											} else
												$message = $this->operation_message->msg_save_fail;									
										}else
											$message = $this->imageupload->error_message;									
									} else {
										if ($this->mod_cafe_contact->edit($this->input->post('ccid'), $cafe_id)){
											$result = 1;
											$message = $this->operation_message->msg_save_success;
										} else
											$message = $this->operation_message->msg_save_fail;
									}
									
								}
								break;
								case 0: default:{									
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2: {									
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;								
							}

							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));
						}
						break;
						case 'ajax_load_previous':{
							$this->load->model('local/data/cafe/mod_cafe_contact');
							$row = $this->mod_cafe_contact->get_single_contact($this->input->post('id'), $this->cafe_id());

							if (!is_null($row))
								print_r(json_encode(array('cct' => $row['CAF_P_L_TITLE'],
															'cca' => $row['CAF_P_L_ADDRESS'],
															'ccpc' => $row['CAF_P_L_POSTCODE'],
															'ccph' => $row['CAF_P_L_PHONE'],
															'ccfx' => $row['CAF_P_L_FAX'],
															'ccem' => $row['CAF_P_L_EMAIL'],
															'ccm' => $row['CAF_P_L_MAP'],
															'ccpv' => $row['PROVINCE_ID'])));
							else
								print null;
						}
						break;
						case 'ajax_remove_item':{
							$result = 0;
							$message = null;
							$redirect_url = null;							
							
							$this->load->library('define/operation_message');							
							switch ($this->check_member_session()){
								
								case 1:{
									$this->load->model('local/data/cafe/mod_cafe_contact');									
									if ($this->mod_cafe_contact->remove($this->input->post('id'))){
										$result = 1;
										$message = $this->operation_message->msg_remove_success;
									} else
										$message = $this->operation_message->msg_remove_fail;										
								}
								break;
								case 0: default:{									
									$result = -1;
									$message = $this->operation_message->msg_relogin;
									$redirect_url = base_url() . 'member/logout';
								}
								break;
								case -2: {									
									$result = -2;
									$message = $this->operation_message->msg_authen_fail;
									$redirect_url = base_url() . 'member/logout';
								}
								break;								
							}
							
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect_url' => $redirect_url)));
						}
						break;
						default:{
							
							if ($this->check_member_session() === 1){								
								$this->pre_backend_data('general/contact');
								$this->set_navigation(array('ระบบจัดการร้าน ' . $this->uri->segment(1) => base_url() . $this->uri->segment(1) . '/backend',
															'ข้อมูลทั่วไป' => base_url() . $this->uri->segment(1) . '/backend/general',
															'ที่ตั้งร้าน' => base_url() . $this->uri->segment(1) . '/backend/general/contact/'));
															
								// load contact list				
								$this->load->model('local/data/cafe/mod_cafe_contact');							
									
								$this->load->library('define/image_upload_define');
								$data_content['cafe_name'] = get_class($this);
								$data_content['data'] = $arr_contact = $this->mod_cafe_contact->get_contact_of_cafe($this->cafe_id());							
								$data_content['image_config'] = array('map_img_limit_height' => $this->image_upload_define->map_img_limit_height,
																	'map_img_limit_width' => $this->image_upload_define->map_img_limit_width,
																	'map_img_limit_size' => $this->image_upload_define->map_img_limit_size,
																	'map_img_extensions' => $this->image_upload_define->map_img_extensions);
									
								$this->template->write('content_2', $this->load->view('cafe/backend/frm_backend_general_contact', $data_content, true));
								$this->template->render();
							} else
								redirect(base_url() . 'member/logout');
						}
					}					
				}
				break;
			}
		}
		private function region_gallery($opr){
				
			$opr = $this->uri->segment(4);
			switch ($opr){
				case 'add':{
					$this->load->library('define/operation_message');
					
					if ($this->check_member_session() === 1){
						$file = $_FILES['fg'];
			
						if ($file['name'] != ''){								
							
							$message = null;								
							$cafe_id = $this->cafe_id();
							
							// get current package detail
							$this->load->model('local/data/cafe/mod_cafe_expire');
							$package = $this->mod_cafe_expire->get_current_package_state($cafe_id, true, true);
								
							$img_limit = $package['CAF_IMG_COUNT'];
								
							// get current image gallery count
							$this->load->model('local/data/cafe/mod_cafe_img_gallery');
							$curr_count = $this->mod_cafe_img_gallery->get_current_img_count($cafe_id);
								
							// check if cafe image count over limitation of pakcage
							if ($curr_count < $img_limit){
								$this->load->library('imageupload');
								$this->load->library('define/image_upload_define');
								$this->load->library('define/directory_path');
			
								$this->imageupload->target_directory = $this->directory_path->img_upload_cafe_gallery_path;
								$this->imageupload->max_image_height = $this->image_upload_define->cafe_img_limit_height;
								$this->imageupload->max_image_width = $this->image_upload_define->cafe_img_limit_width;
								$this->imageupload->max_image_size = $this->image_upload_define->cafe_img_limit_size;
								$this->imageupload->image_extensions = $this->image_upload_define->cafe_img_extensions;
								$this->imageupload->prefix = $cafe_id;
									
								if ($this->imageupload->upload($file)){								
									$this->mod_cafe_img_gallery->CAF_IMG_FILENAME = $this->imageupload->encode_filename($file['name']);
									$this->mod_cafe_img_gallery->CAF_ID = $cafe_id;
									if (!$this->mod_cafe_img_gallery->add())
										$message = $this->operation_message->msg_save_fail;
									else
										$this->mod_cafe->set_last_active(get_class($this));
								}else
									$message = $this->imageupload->error_message;
							}else
								$message = $this->operation_message->msg_image_over_limit . ' ' . $img_limit . ' รูป';
						}else
							$message = null;
			
						$this->jscript_redirect(base_url() . get_class($this) . '/backend/gallery/', $message);
					} else 
						$this->jscript_redirect(base_url() . 'member/logout', $this->operation_message->msg_relogin);
				}
				break;
				case 'ajax_remove_item':{					
					$result = 0;
					$message = null;
					$redirect_url = null;
							
					$this->load->library('define/operation_message');							
					switch ($this->check_member_session()){
								
						case 1:{																
							$filename = $this->input->post('fname');
		
							$this->load->model('local/data/cafe/mod_cafe_img_gallery');
							if ($this->mod_cafe_img_gallery->remove($filename, $this->cafe_id())){
								$this->mod_cafe->set_last_active(get_class($this));				
								$result = 1;
								$message = $this->operation_message->msg_remove_success;
							} else 
								$message = $this->operation_message->msg_remove_fail;
						}
						break;
						case 0: default:{									
							$result = -1;
							$message = $this->operation_message->msg_relogin;
							$redirect_url = base_url() . 'member/logout';
						}
						break;
						case -2: {									
							$result = -2;
							$message = $this->operation_message->msg_authen_fail;
							$redirect_url = base_url() . 'member/logout';
						}
						break;								
					}

					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'redirect_url' => $redirect_url)));
				}
				break;
				case 'ajax_remove_select':{					
					$result = 0;
					$message = null;
					$redirect_url = null;
							
					$this->load->library('define/operation_message');							
					switch ($this->check_member_session()){
								
						case 1:{																
							$flist = $this->input->post('flist');
		
							if (count($flist) > 0) {
								$this->load->model('local/data/cafe/mod_cafe_img_gallery');
				
								if ($this->mod_cafe_img_gallery->remove_multiple($flist, $this->cafe_id())){
									$result = 1;
									$message = $this->operation_message->msg_remove_success;
								} else 
									$message = $this->operation_message->msg_remove_fail;
							} else
								$message = $this->operation_message->msg_remove_fail;
						}
						break;
						case 0: default:{									
							$result = -1;
							$message = $this->operation_message->msg_relogin;
							$redirect_url = base_url() . 'member/logout';
						}
						break;
						case -2: {									
							$result = -2;
							$message = $this->operation_message->msg_authen_fail;
							$redirect_url = base_url() . 'member/logout';
						}
						break;								
					}

					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'redirect_url' => $redirect_url)));
				}
				break;
				default:{
					
					if ($this->check_member_session() === 1){
						$this->pre_backend_data('gallery');
						$this->set_navigation(array('ระบบจัดการร้าน ' . $this->uri->segment(1) => base_url() . $this->uri->segment(1) . '/backend',
													'รูปภาพร้าน' => base_url() . $this->uri->segment(1) . '/backend/gallery'));															
															
						$this->load->model('local/data/cafe/mod_cafe_img_gallery');
							
						$this->load->library('define/image_upload_define');						
						$data_content['cafe_name'] = get_class($this);
						$data_content['data'] = $this->mod_cafe_img_gallery->get_gallery_list($this->cafe_id(), true);
						$data_content['image_config'] = array('cafe_img_limit_height' => $this->image_upload_define->cafe_img_limit_height,
																	'cafe_img_limit_width' => $this->image_upload_define->cafe_img_limit_width,
																	'cafe_img_limit_size' => $this->image_upload_define->cafe_img_limit_size,
																	'cafe_img_extensions' => $this->image_upload_define->cafe_img_extensions);
						
						$this->template->write('content_2', $this->load->view('cafe/backend/frm_backend_gallery', $data_content, true));
						$this->template->render();
					} else
						redirect(base_url() . 'member/logout');
				}
				break;
			}
		}
		private function region_product($opr){
						
			switch ($opr){
				case 'ajax_add':{										
					$cafe_id = $this->cafe_id();
					
					$this->load->library('define/operation_message');
			
					// get current package detail
					$this->load->model('local/data/cafe/mod_cafe_expire');
					$package = $this->mod_cafe_expire->get_current_package_state($cafe_id, true, true);						
					$product_limit = $package['CAF_PDT_COUNT'];
						
					// get current product count
					$this->load->model('local/data/cafe/mod_cafe_product');
					$curr_count = $this->mod_cafe_product->get_count($cafe_id);
			
					// check if cafe image count over limitation of pakcage
					$result = 0;
					$message = null;
					$redirect_url = null;					
					if ($curr_count < $product_limit){					
						
						$this->load->model('local/data/cafe/mod_cafe_product');
						$this->mod_cafe_product->CAF_PDT_NAME = $this->input->post('cpn');
						$this->mod_cafe_product->CAF_PDT_CODE = $this->mod_cafe_product->gen_product_code($cafe_id, $this->input->post('cpcat'));
						$this->mod_cafe_product->CAF_PDT_PRICE = $this->input->post('cpp');
						$this->mod_cafe_product->CAF_PDT_DC = $this->input->post('cpdc');
						$this->mod_cafe_product->CAF_PDT_DESC = $this->input->post('cpdesc');
						$this->mod_cafe_product->CAF_PDT_CAT_ID = $this->input->post('cpcat');
						$this->mod_cafe_product->CAF_ID = $cafe_id;
									
						$upload_pass = true;
						if (isset($_FILES['cpimg']) && !empty($_FILES['cpimg']['name'])){
							$file = $_FILES['cpimg'];
							$this->load->library('imageupload');
							$this->load->library('define/image_upload_define');
							$this->load->library('define/directory_path');
			
							$this->imageupload->target_directory = $this->directory_path->img_upload_cafe_product_path;
							$this->imageupload->max_image_height = $this->image_upload_define->pdt_img_limit_height;
							$this->imageupload->max_image_width = $this->image_upload_define->pdt_img_limit_width;
							$this->imageupload->max_image_size = $this->image_upload_define->pdt_img_limit_size;
							$this->imageupload->image_extensions = $this->image_upload_define->pdt_img_extensions;
							$this->imageupload->prefix = $cafe_id;
								
							if (!$this->imageupload->upload($file)){
								$upload_pass = false;
								$message = $this->imageupload->error_message;
							}
			
							$this->mod_cafe_product->CAF_PDT_IMG = $this->imageupload->encode_filename($file['name']);
						}
			
						if ($upload_pass){
							if ($this->mod_cafe_product->add()){							
								$result = 1;
								$message = $this->operation_message->msg_save_success;
								
								$this->mod_cafe->set_last_active(get_class($this));
							} else
								$message = $this->operation_message->msg_save_fail;
						}
					}
					else
						$message = $this->operation_message->msg_image_over_limit . ' ' . $product_limit . ' ชิ้น';
			
					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'redirect_url' => $redirect_url)));
				}
				break;
				case 'ajax_edit':{					
					$this->load->library('define/operation_message');				
			
					// check if cafe image count over limitation of pakcage
					$result = 0;
					$message = null;
					$redirect_url = null;
						
					$this->load->model('local/data/cafe/mod_cafe_product');
					$this->mod_cafe_product->CAF_PDT_NAME = $this->input->post('cpn');					
					$this->mod_cafe_product->CAF_PDT_PRICE = $this->input->post('cpp');
					$this->mod_cafe_product->CAF_PDT_DC = $this->input->post('cpdc');
					$this->mod_cafe_product->CAF_PDT_DESC = $this->input->post('cpdesc');
					$this->mod_cafe_product->CAF_PDT_CAT_ID = $this->input->post('cpcat');					
									
					$upload_pass = true;
					if (isset($_FILES['cpimg']) && !empty($_FILES['cpimg']['name'])){
						$file = $_FILES['cpimg'];
						$this->load->library('imageupload');
						$this->load->library('define/image_upload_define');
						$this->load->library('define/directory_path');
			
						$this->imageupload->target_directory = $this->directory_path->img_upload_cafe_product_path;
						$this->imageupload->max_image_height = $this->image_upload_define->pdt_img_limit_height;
						$this->imageupload->max_image_width = $this->image_upload_define->pdt_img_limit_width;
						$this->imageupload->max_image_size = $this->image_upload_define->pdt_img_limit_size;
						$this->imageupload->image_extensions = $this->image_upload_define->pdt_img_extensions;
						$this->imageupload->prefix = $this->cafe_id();
								
						if (!$this->imageupload->upload($file)){
							$upload_pass = false;
							$message = $this->imageupload->error_message;
						}
						$this->mod_cafe_product->CAF_PDT_IMG = $this->imageupload->encode_filename($file['name']);
					}
			
					if ($upload_pass){
						if ($this->mod_cafe_product->edit($this->input->post('cpid'))){							
							$result = true;
							$message = $this->operation_message->msg_save_success;
							
							$this->mod_cafe->set_last_active(get_class($this));
						} else
							$message = $this->operation_message->msg_save_fail;
					}					
			
					print_r(json_encode(array('result' => $result,
												'message' => $message)));
				}
				break;
				case 'ajax_remove_item':{						
					$this->load->model('local/data/cafe/mod_cafe_product');
					
					$this->load->library('define/operation_message');
					if ($this->mod_cafe_product->remove($this->input->post('id')))
						print_r(json_encode(array('result' => true,
													'message' => $this->operation_message->msg_remove_success)));
					else
						print_r(json_encode(array('result' => false,
													'message' => $this->operation_message->msg_remove_fail)));
				}
				break;
				case 'ajax_remove_select':{						
					$this->load->model('local/data/cafe/mod_cafe_product');
					
					$this->load->library('define/operation_message');
					if ($this->mod_cafe_product->remove_in($this->input->post('pl')))
						print_r(json_encode(array('result' => true,
													'message' => $this->operation_message->msg_remove_success)));
					else
						print_r(json_encode(array('result' => false,
													'message' => $this->operation_message->msg_remove_fail)));					
				}
				break;
				case 'ajax_load_previous':{
					$product_id = $this->input->post('id');
			
					$this->load->model("local/data/cafe/mod_cafe_product");
					$row = $this->mod_cafe_product->get_single_product($product_id);
			
					print_r(json_encode(array('cpn' => $row['CAF_PDT_NAME'],
												'cpp' => $row['CAF_PDT_PRICE'],
												'cpdc' => $row['CAF_PDT_DC'],
												'cpdesc' => $row['CAF_PDT_DESC'],
												'cpimg' => $row['CAF_PDT_IMG'],
												'cpcat' => $row['CAF_PDT_CAT_NAME'])));
				}
				break;
				default:{
					if ($this->check_member_session() === 1){
						$this->pre_backend_data('product');
						$this->set_navigation(array('ระบบจัดการร้าน ' . $this->uri->segment(1) => base_url() . $this->uri->segment(1) . '/backend',
													'สินค้า' => base_url() . $this->uri->segment(1) . '/backend/product'));
													
						$category_id = $this->input->get('c');
						$start_index = $this->input->get('p');
						$display_length = 10;
						$cafe_id = $this->cafe_id();
						
						if (empty($category_id))
							$category_id = null;
						if (empty($start_index))
							$start_index = null;
						else{
							$start_index = ($start_index -1) * $display_length;
							if ($start_index < 0)
								$start_index = 0;
						}
						
						$this->load->model('local/data/cafe/mod_cafe_product');
						$this->load->model('local/data/cafe/mod_cafe_product_category');
						$this->load->model('local/data/article/mod_article');
						$this->load->library('define/image_upload_define');
							
						$data_content['cafe_name'] = get_class($this);
						$data_content['data'] = $this->mod_cafe_product->get_product_list($cafe_id, $category_id, $display_length, $start_index);
						$data_content['categories'] = $this->mod_cafe_product_category->get_category_list(true);
						$data_content['max_count'] = ceil($this->mod_cafe_product->get_count($cafe_id, $category_id) / $display_length);
						$data_content['current_category'] = $category_id;
						$data_content['image_config'] = array('pdt_img_limit_height' => $this->image_upload_define->pdt_img_limit_height,
																	'pdt_img_limit_width' => $this->image_upload_define->pdt_img_limit_width,
																	'pdt_img_limit_size' => $this->image_upload_define->pdt_img_limit_size,
																	'pdt_img_extensions' => $this->image_upload_define->pdt_img_extensions); 
						
						$this->template->write('content_2', $this->load->view('cafe/backend/frm_backend_product', $data_content, true));
						$this->template->render();
					} else 
						redirect(base_url() . 'member/logout');
				}
				break;
			}
		}
		private function region_package($opr){
			switch ($opr){
				case 'ajax_check_package':{
					$result = 0;
					$message = null;
					$message_2 = null;
					$price = null;
					$redirect_url = null;
					
					$this->load->library('define/operation_message');							
					switch ($this->check_member_session()){
								
						case 1:{																
							$this->load->model('local/data/mod_package');
							$free_package = $this->mod_package->get_free_package_detail();
							
							$this->load->library('define/operation_message');
								
							// get previous package detail
							$this->load->model('local/data/cafe/mod_cafe_expire');
							$current_package = $this->mod_cafe_expire->get_current_package_state($this->cafe_id(), false, true);
							
							// new package order
							$order_package = $this->mod_package->get_package($this->input->post('opk'), array('PAK_NAME'));
							$price = $this->mod_package->calculate_price($this->input->post('opk'), $this->input->post('odday'));
								
							if ($current_package['PAK_ID'] == $free_package['PAK_ID']){								
								$result = 1;
								$message = $this->operation_message->msg_order_package($order_package['PAK_NAME'], $this->input->post('odday'), $price);
								$price = $price;
							} else {
								switch ($this->mod_package->compare_package($current_package['PAK_ID'], $this->input->post('opk'))){					
									
									case -1: case 1:{															
										$result = 1;
										$message = $this->operation_message->msg_change_package($current_package['PAK_NAME'], $current_package['DAY_REMAIN']);
										$message_2 = $this->operation_message->msg_order_package($order_package['PAK_NAME'], $this->input->post('odday'), $price );
										$price = $price;
									}
									break;
									case 0: default:{									
										$message = $this->operation_message->msg_extend_package_day($current_package['PAK_NAME'], $current_package['DAY_REMAIN'], $this->input->post('odday'), $price );
										$price = $price;
									}
									break;
								}
							}
						}
						break;
						case 0: default:{									
							$result = -1;
							$message = $this->operation_message->msg_relogin;
							$redirect_url = base_url() . 'member/logout';
						}
						break;
						case -2: {									
							$result = -2;
							$message = $this->operation_message->msg_authen_fail;
							$redirect_url = base_url() . 'member/logout';
						}
						break;								
					}

					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'message_2' => $message_2,
												'price' => $price,
												'redirect_url' => $redirect_url)));										
				}
				break;
				case 'ajax_order':{					

					$this->load->model('local/data/mod_package_order');
					$this->mod_package_order->PAK_O_DAY_AMOUNT = $this->input->post('odday');					
					$this->mod_package_order->PAK_ID = $this->input->post('odpk');
					$this->mod_package_order->MEM_ID = $this->session->userdata('MEM_ID');
					$this->mod_package_order->CAF_ID = $this->cafe_id();
					
					$this->load->library('define/operation_message');
					if ($this->mod_package_order->add())
						print_r(json_encode(array('result' => true,
													'message' => $this->operation_message->msg_save_success)));
					else
						print_r(json_encode(array('result' => false,
													'message' => $this->operation_message->msg_save_fail)));
				}
				break;
				case 'ajax_get_package':{
					$this->load->model('local/data/mod_package');
					$result = $this->mod_package->get_package_list();
								
					$return = array();
					for ($i=0; $i<count($result); $i++)
						array_push($return, array('id' => $result[$i]['PAK_ID'],
													'name' => $result[$i]['PAK_NAME']));
					print_r(json_encode($return));
				}
				break;
				default:{
					if ($this->check_member_session() === 1){
						$this->pre_backend_data('package');
						$this->set_navigation(array('ระบบจัดการร้าน ' . $this->uri->segment(1) => base_url() . $this->uri->segment(1) . '/backend',
													'แพ็กเก็จ' => base_url() . $this->uri->segment(1) . '/backend/package'));
													
						$cafe_id = $this->cafe_id();
				
						$this->load->model('local/data/cafe/mod_cafe_expire');						
						$this->load->model('local/data/cafe/mod_cafe_img_gallery');
						$this->load->model('local/data/cafe/mod_cafe_product');						
						$this->load->model('local/data/cafe/mod_cafe_img_cover');					
									
						$data_content['data'] = $this->mod_cafe_expire->get_current_package_state($this->cafe_id(), true, true);					
						$data_content['curr_gallery_count'] = $this->mod_cafe_img_gallery->get_current_img_count($cafe_id);
						$data_content['curr_product_count'] = $this->mod_cafe_product->get_count($cafe_id);
						$data_content['curr_cover_count'] = $this->mod_cafe_img_cover->get_current_img_count($cafe_id);
						$data_content['cafe_title'] = $this->mod_cafe->get_cafe_title_by_name(get_class($this));
									
						$this->template->write('content_1', $this->load->view('cafe/backend/frm_backend_package', $data_content, true));
						$this->template->render();
					} else
						redirect(base_url() . 'member/logout');
				}
				break;	
			}	
		}	
				
		private function authenrize(){
			$this->load->model("local/data/cafe/mod_cafe");
			return $this->mod_cafe->check_cafe_owner(get_class($this), $this->session->userdata('MEM_ID'));
		}
		
		function ajax_add_favorite_cafe(){
			$result = 0;
			$message = null;
			
			$this->load->library('define/operation_message');
			if ($this->check_member_session()){				
				$this->load->model('local/data/mod_member');			
				
				if ($this->mod_member->add_favorite_cafe($this->cafe_id(), $this->session->userdata('MEM_ID'))){
					// increase favorite number
					$this->load->model('local/data/cafe/mod_cafe');
					$this->mod_cafe->increase_favorite($this->cafe_id());
					
					$result = 1;
					$message = $this->operation_message->msg_add_cafe_favorite;
				} else 
					$message = $this->operation_message->msg_save_fail;
			} else{
				$result = -1;
				$message = $this->operation_message->msg_relogin;
			}
			
			print_r(json_encode(array('result' => $result,
										'message' => $message)));	
		}
		function ajax_remove_favorite_cafe(){
			$result = 0;
			$message = null;
			
			if ($this->check_member_session()){				
				
				$this->load->library('define/operation_message');
				$this->load->model('local/data/mod_member');
				
				if ($this->mod_member->remove_favorite_cafe($this->cafe_id(), $this->session->userdata('MEM_ID'))){
					// decrease favorite number
					$this->load->model('local/data/cafe/mod_cafe');
					$this->mod_cafe->decrease_favorite($this->cafe_id());
					
					$result = true;
					$message = $this->operation_message->msg_remove_cafe_favorite;
				} else 
					$message = $this->operation_message->msg_save_fail;
			} else{
				$result = -1;
				$message = $this->operation_message->msg_relogin;
			} 
						
			print_r(json_encode(array('result' => $result,
										'message' => $message)));			
		}
		function ajax_save_product_record_rate(){
			$mem_id = $this->session->userdata('MEM_ID');
			$product_code = $this->input->post('p');
			$score = $this->input->post('s');						
		
			if (!isset($mem_id))
				print $this->operation_message->msg_no_member_login;
			else
			{
				$this->load->model('local/data/mod_member');			
				if ($this->mod_member->check_member_exist($mem_id))
				{
					$product_code = substr($product_code, 1, strlen($product_code)-2);
					
					$this->load->model('local/data/cafe/mod_cafe_product');
					$product_id = $this->mod_cafe_product->get_product_id_from_product_code($product_code);
					
					if (!is_null($product_id))
					{
						if ($score >= 0 && $score <= 5)
						{
							$this->load->model('local/data/rate/mod_p_rate_record');
							$this->mod_p_rate_record->P_RATE_RC_SCORE = $score;
							$this->mod_p_rate_record->CAF_PDT_ID = $product_id;
							$this->mod_p_rate_record->CAF_ID = $cafe_id;
							$this->mod_p_rate_record->MEM_ID = $mem_id;
							
							// check if user not vote
							$result = $this->mod_p_rate_record->get(array('CAF_ID' => $cafe_id, 'CAF_PDT_ID' => $product_id, 'MEM_ID' => $mem_id));
							$pass = false;					
							
							if ($result->count() > 0)											
								$pass = $this->mod_p_rate_record->edit(array('CAF_ID' => $cafe_id, 'CAF_PDT_ID' => $product_id, 'MEM_ID' => $mem_id));							
							else					
								$pass = $this->mod_p_rate_record->add();					
		
							// save result
							if ($pass)
							{
								$this->load->model('local/data/rate/mod_p_rate_result');						
								 if ($this->mod_p_rate_result->is_rated_product($product_id, $cafe_id))						 
								 	$this->mod_p_rate_result->update_freq($score, null, $product_id, $cafe_id);					 
								 else
								 {
								 	$new_score = $score;
								 	$attr = 'P_RATE_RT_SCORE_' . $score;
									$old_score = $result[0]->$attr;
									print $attr;
								 	$this->mod_p_rate_result->update_freq($new_score, $old_score, $product_id, $cafe_id);
								 }
							}
							
							print $this->operation_message->msg_rate_success;
						}
						else
							print $this->operation_message->msg_error;
					}
					else
						print $this->operation_message->msg_error;
				}
				else
					print $this->operation_message->msg_no_member_exist;
			}
		}
	}
?>